

/*删除*/


import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo07_DBUtil {
    public static void main(String[] args) {
        //声明对象
        Connection connection=null;
        PreparedStatement statement=null;

        try {
            //获取数据库连接
            connection=DBUtil.getConnection();
            //定义SQL语句
            String sql="delete from student where id = ?";
            //定义SQL的预处理对象
            statement=connection.prepareStatement(sql);
            //接收用户输入
            Scanner scanner=new Scanner(System.in);
            System.out.println("输入要删除的学生学号：");
            Long inputId=scanner.nextLong();
            //用真实数据替代占位符
            statement.setLong(1,inputId);
            //执行SQL语句
            int row=statement.executeUpdate();
            //处理结果
            if(row==1){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            DBUtil.close(null,statement,connection);
        }
    }

}
